home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
-
- /* blendimg.c
- * Demonstrates how to use the GL_EXT_blend_color extensions
- * to blend two images that do not have an alpha channel.
- *
- * Up Arrow - increase blend factor
- * Down Arrow - decrease blend factor
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "rgbImageFile.h" /* should be found in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
-
- GLvoid printHelp( char *progname );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat blend_factor = 0.5;
-
- static unsigned int *image1, *image2;
- static int imgwidth1, imgheight1, imgwidth2, imgheight2;
-
- void
- main ( int argc, char *argv[])
- {
- GLsizei width, height;
- char *imgfile[2];
- char *defaultImage[] = { "aztecNM.rgb", "bricks.rgb" };
-
-
- glutInit( &argc, argv );
-
- if (argc < 3) {
- imgfile[0] = ( argc > 1 ? argv[1] : defaultImage[0]);
- imgfile[1] = ( argc > 2 ? argv[2] : defaultImage[1]);
- } else {
- imgfile[0] = argv[1];
- imgfile[1] = argv[2];
- }
-
- image1 = rgbReadImageFile(imgfile[0], &imgwidth1, &imgheight1);
- image2 = rgbReadImageFile(imgfile[1], &imgwidth2, &imgheight2);
-
- width = ( imgwidth1 > imgwidth2 ? imgwidth1 : imgwidth2 );
- height = ( imgheight1 > imgheight2 ? imgheight1 : imgheight2 );
-
- glutInitWindowPosition( 100, 100 );
- glutInitWindowSize( width, height );
- glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s, a program to blend two RGB images\n\n"
- "UP Arrow - increase blend factor\n"
- "DOWN Arrow - decrease blend factor\n"
- "Escape key - exit the program\n\n",
- progname);
- fprintf(stdout, "blend_factor = %4.2f\n", blend_factor );
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_UP: /* increase blend factor */
- if (blend_factor < 1.0) blend_factor += 0.1;
- break;
- case GLUT_KEY_DOWN: /* decrease blend factor */
- if (blend_factor > 0.0) blend_factor -= 0.1;
- break;
- }
- printf( "blend_factor = %4.2f\n", blend_factor );
- glutPostRedisplay();
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width, height );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) width, 0.0, (GLdouble) height );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT );
-
- /* draw first image without blending */
- glDisable( GL_BLEND );
- glRasterPos2i( 0, 0 );
- glDrawPixels( imgwidth1, imgheight1, GL_RGBA, GL_UNSIGNED_BYTE,
- image1 );
-
- /* draw second image with constant alpha */
- glEnable( GL_BLEND );
- glBlendColorEXT( 0, 0, 0, blend_factor );
- glBlendFunc( GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT );
- glDrawPixels( imgwidth2, imgheight2, GL_RGBA, GL_UNSIGNED_BYTE,
- image2 );
-
- glFlush();
- }
-